ENMTools

This package implements various tests, visualizations, and metrics for use with environmental niche models (ENMs) and species distribution models (SDMs).


Installation

At present, ENMTools is downloadable from https://github.com/danlwarren/ENMTools. There are multiple ways to download it. The easiest is to use devtools and install from GitHub.

Installing from GitHub using devtools

Run the following code from your R console:

install.packages("devtools")
library(devtools)
install_github("danlwarren/ENMTools")
library(ENMTools)

Install from zip file

A zipped version of the package is available at https://github.com/danlwarren/ENMTools/archive/master.zip. To install from the zip file, download a copy of it to your system. Once it’s finished downloading, type the following (where PATH is the path to the zip file):

install.packages("devtools")
library(devtools)
install_local("PATH")
library(ENMTools)

Interacting with ENMTools

Creating enmtools.species objects

First we’re going to load in some environmental data.

env.files <- list.files(path = "test/testdata/", pattern = "pc", full.names = TRUE)
env <- stack(env.files)
names(env) <- c("layer.1", "layer.2", "layer.3", "layer.4")
env <- setMinMax(env)

ENMTools is primarily designed to examine patterns of similarity and difference between ENMs for different species. In order to simplify interactions with the functions in ENMTools, you need to put your data for each of your species into an enmtools.species object. You can create and view an empty enmtools.species object just by typing:

ahli <- enmtools.species()
ahli
## 
## 
## Range raster not defined.
## 
## Presence points not defined.
## 
## Background points not defined.
## 
## Species name not defined.

You can add data to this object manually:

names(ahli)
## [1] "range"             "presence.points"   "background.points"
## [4] "models"            "species.name"
ahli$species.name <- "ahli"
ahli$presence.points <- read.csv("test/testdata/ahli.csv")[,3:4]
ahli$range <- background.raster.buffer(ahli$presence.points, 50000, mask = env)
ahli$background.points <- background.points.buffer(points = ahli$presence.points,
                                                   radius = 20000, n = 1000, mask = env[[1]])

ahli
## 
## 
## Range raster: 
## class       : RasterLayer 
## dimensions  : 418, 1535, 641630  (nrow, ncol, ncell)
## resolution  : 0.008333333, 0.008333333  (x, y)
## extent      : -86.90809, -74.11642, 19.80837, 23.2917  (xmin, xmax, ymin, ymax)
## coord. ref. : NA 
## data source : in memory
## names       : layer.1 
## values      : 1, 1  (min, max)
## 
## 
## 
## Presence points (first ten only): 
## 
##  Longitude   Latitude
## ----------  ---------
##   -80.0106    21.8744
##   -79.9086    21.8095
##   -79.8065    21.7631
##   -79.8251    21.8095
##   -79.8807    21.8374
##   -79.9550    21.8374
##   -80.3446    22.0136
##   -80.2983    21.9951
##   -80.1776    21.9023
##   -80.1591    21.9673
## 
## 
## Background points (first ten only): 
## 
##  Longitude   Latitude
## ----------  ---------
##  -79.77892   21.72920
##  -80.13726   22.13754
##  -79.76226   21.94587
##  -80.20392   21.96254
##  -80.02059   21.91254
##  -79.82892   21.77087
##  -79.92059   21.96254
##  -80.01226   21.78754
##  -79.92059   21.91254
##  -79.70392   21.70420
## 
## 
## Species name:  ahli

Or you can add bits of it when the object is created:

allogus <- enmtools.species(species.name = "allogus", 
                            presence.points = read.csv("test/testdata/allogus.csv")[,3:4])

allogus$range <- background.raster.buffer(allogus$presence.points, 50000, mask = env)
allogus$background.points <- background.points.buffer(points = allogus$presence.points,
                                                      radius = 20000, n = 1000, mask = env[[1]])

allogus
## 
## 
## Range raster: 
## class       : RasterLayer 
## dimensions  : 418, 1535, 641630  (nrow, ncol, ncell)
## resolution  : 0.008333333, 0.008333333  (x, y)
## extent      : -86.90809, -74.11642, 19.80837, 23.2917  (xmin, xmax, ymin, ymax)
## coord. ref. : NA 
## data source : in memory
## names       : layer.1 
## values      : 1, 1  (min, max)
## 
## 
## 
## Presence points (first ten only): 
## 
##  Longitude   Latitude
## ----------  ---------
##   -79.2527    22.2109
##   -78.7774    22.2241
##   -78.6189    22.2373
##   -78.1039    21.1809
##   -78.0247    21.1809
##   -77.9983    20.9301
##   -77.9719    21.7091
##   -77.9719    21.5507
##   -77.9323    21.6167
##   -77.9323    20.7320
## 
## 
## Background points (first ten only): 
## 
##  Longitude   Latitude
## ----------  ---------
##  -74.45392   20.27087
##  -78.16226   21.17087
##  -76.75392   20.22920
##  -77.99559   21.43754
##  -77.97892   21.75420
##  -75.71226   20.57087
##  -78.25392   21.09587
##  -74.42892   20.23754
##  -77.04559   20.20420
##  -74.78726   20.34587
## 
## 
## Species name:  allogus

Building an ENM

ENMTools contains functions to simplify the ENM construction process. Using enmtools.species objects and the correct modeling commands, we can build models very quickly. These commands are primarily wrappers to dismo model construction and projection functions, and at present are only available for GLM, Maxent, Domain, and Bioclim models. One of the nice bits about this setup is that it allows enmtools to automatically generate suitability maps, do model evaluation, and plot the marginal suitability of habitat for each variable separately.

GLM

GLMs usually require the user to supply a formula, an enmtools.species object, and some environmental data. If your formula is a strictly additive function of all of the environmental layers in env, though, enmtools.glm will build a formula automatically.

ahli.glm <- enmtools.glm(species = ahli, env = env, f = pres ~ layer.1 + layer.2 + layer.3 + layer.4, test.prop = 0.2)
## Adding environmental data to species ahli 
##  Processing presence points...
##  Processing background points...
allogus.glm <- enmtools.glm(species = allogus, env = env, f = pres ~ layer.1 + layer.2 + layer.3 + layer.4, test.prop = 0.2)
## Adding environmental data to species allogus 
##  Processing presence points...
##  Processing background points...
ahli.glm
## 
## 
## Formula:  presence ~ layer.1 + layer.2 + layer.3 + layer.4
## <environment: 0x7ff981880238>
## 
## 
## Data table (top ten lines): 
## 
##       Longitude   Latitude   layer.1   layer.2   layer.3   layer.4   presence
## ---  ----------  ---------  --------  --------  --------  --------  ---------
## 1      -80.0106    21.8744      2765      1235      1174       252          1
## 2      -79.9086    21.8095      2289      1732       957       231          1
## 3      -79.8065    21.7631      2158      1870       983       253          1
## 4      -79.8251    21.8095      2207      1877       967       259          1
## 5      -79.8807    21.8374      2244      1828       945       249          1
## 6      -79.9550    21.8374      2250      1766       919       235          1
## 9      -80.1776    21.9023      2287      1722       992       266          1
## 11     -80.1498    21.9858      3042       841      1371       221          1
## 12     -80.1220    21.9301      2898      1033      1231       242          1
## 13     -80.1776    21.9673      2914      1020      1256       237          1
## 
## 
## Model:  
## Call:
## glm(formula = f, family = "binomial", data = analysis.df[, -c(1, 
##     2)], weights = weights)
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -0.26574  -0.14199  -0.09288  -0.06118   1.39946  
## 
## Coefficients:
##              Estimate Std. Error z value Pr(>|z|)
## (Intercept) 67.858385  51.024981   1.330    0.184
## layer.1     -0.015510   0.012326  -1.258    0.208
## layer.2     -0.013300   0.012809  -1.038    0.299
## layer.3      0.001732   0.014661   0.118    0.906
## layer.4     -0.041880   0.042450  -0.987    0.324
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 33.271  on 1011  degrees of freedom
## Residual deviance: 26.214  on 1007  degrees of freedom
## AIC: 21.966
## 
## Number of Fisher Scoring iterations: 5
## 
## 
## 
## Model fit (training data):  class          : ModelEvaluation 
## n presences    : 12 
## n absences     : 1000 
## AUC            : 0.7846667 
## cor            : 0.1017259 
## max TPR+TNR at : -0.5083943 
## 
## 
## Environment space model fit (training data):  class          : ModelEvaluation 
## n presences    : 12 
## n absences     : 10000 
## AUC            : 0.528225 
## cor            : 0.009501957 
## max TPR+TNR at : 0.3754935 
## 
## 
## Proportion of data wittheld for model fitting:  0.2
## 
## Model fit (test data):  class          : ModelEvaluation 
## n presences    : 4 
## n absences     : 1000 
## AUC            : 0.65925 
## cor            : 0.02868907 
## max TPR+TNR at : -0.5902837 
## 
## 
## Environment space model fit (test data):  class          : ModelEvaluation 
## n presences    : 4 
## n absences     : 10000 
## AUC            : 0.485975 
## cor            : -0.001574794 
## max TPR+TNR at : 0.3564927 
## 
## 
## Suitability:  
## class       : RasterLayer 
## dimensions  : 418, 1535, 641630  (nrow, ncol, ncell)
## resolution  : 0.008333333, 0.008333333  (x, y)
## extent      : -86.90809, -74.11642, 19.80837, 23.2917  (xmin, xmax, ymin, ymax)
## coord. ref. : NA 
## data source : in memory
## names       : layer 
## values      : 2.220446e-16, 1  (min, max)
## 
## 
## 
## Notes:

Notice this produces the same formula as:

ahli.glm <- enmtools.glm(species = ahli, env = env, test.prop = 0.2)
## Adding environmental data to species ahli 
##  Processing presence points...
##  Processing background points...
ahli.glm
## 
## 
## Formula:  presence ~ layer.1 + layer.2 + layer.3 + layer.4
## <environment: 0x7ff9961b4148>
## 
## 
## Data table (top ten lines): 
## 
##       Longitude   Latitude   layer.1   layer.2   layer.3   layer.4   presence
## ---  ----------  ---------  --------  --------  --------  --------  ---------
## 1      -80.0106    21.8744      2765      1235      1174       252          1
## 3      -79.8065    21.7631      2158      1870       983       253          1
## 5      -79.8807    21.8374      2244      1828       945       249          1
## 6      -79.9550    21.8374      2250      1766       919       235          1
## 7      -80.3446    22.0136      2201      1822       978       277          1
## 9      -80.1776    21.9023      2287      1722       992       266          1
## 11     -80.1498    21.9858      3042       841      1371       221          1
## 12     -80.1220    21.9301      2898      1033      1231       242          1
## 13     -80.1776    21.9673      2914      1020      1256       237          1
## 14     -80.2148    21.9394      2329      1692      1018       269          1
## 
## 
## Model:  
## Call:
## glm(formula = f, family = "binomial", data = analysis.df[, -c(1, 
##     2)], weights = weights)
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -0.25149  -0.14748  -0.09477  -0.05877   1.49526  
## 
## Coefficients:
##               Estimate Std. Error z value Pr(>|z|)  
## (Intercept) 89.4188799 54.3154151   1.646   0.0997 .
## layer.1     -0.0209583  0.0128621  -1.629   0.1032  
## layer.2     -0.0216285  0.0137751  -1.570   0.1164  
## layer.3     -0.0036544  0.0149485  -0.244   0.8069  
## layer.4     -0.0004819  0.0395900  -0.012   0.9903  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 33.271  on 1011  degrees of freedom
## Residual deviance: 26.315  on 1007  degrees of freedom
## AIC: 22.031
## 
## Number of Fisher Scoring iterations: 5
## 
## 
## 
## Model fit (training data):  class          : ModelEvaluation 
## n presences    : 12 
## n absences     : 1000 
## AUC            : 0.770625 
## cor            : 0.09766875 
## max TPR+TNR at : 0.03117087 
## 
## 
## Environment space model fit (training data):  class          : ModelEvaluation 
## n presences    : 12 
## n absences     : 10000 
## AUC            : 0.4897167 
## cor            : 0.007018582 
## max TPR+TNR at : 0.3268632 
## 
## 
## Proportion of data wittheld for model fitting:  0.2
## 
## Model fit (test data):  class          : ModelEvaluation 
## n presences    : 4 
## n absences     : 1000 
## AUC            : 0.705625 
## cor            : 0.04256084 
## max TPR+TNR at : 0.375986 
## 
## 
## Environment space model fit (test data):  class          : ModelEvaluation 
## n presences    : 4 
## n absences     : 10000 
## AUC            : 0.47485 
## cor            : 0.00127188 
## max TPR+TNR at : 0.2512343 
## 
## 
## Suitability:  
## class       : RasterLayer 
## dimensions  : 418, 1535, 641630  (nrow, ncol, ncell)
## resolution  : 0.008333333, 0.008333333  (x, y)
## extent      : -86.90809, -74.11642, 19.80837, 23.2917  (xmin, xmax, ymin, ymax)
## coord. ref. : NA 
## data source : in memory
## names       : layer 
## values      : 1.451537e-06, 1  (min, max)
## 
## 
## 
## Notes:

If you want a more complicated formula, though (e.g., with interactions or polynomial effects), you’ll need to supply that manually.

ahli.glm <- enmtools.glm(species = ahli, env = env, f = pres ~ poly(layer.1, 2) + poly(layer.2, 2) + poly(layer.3, 2) + poly(layer.4, 2), test.prop = 0.2)
## Adding environmental data to species ahli 
##  Processing presence points...
##  Processing background points...
ahli.glm
## 
## 
## Formula:  presence ~ poly(layer.1, 2) + poly(layer.2, 2) + poly(layer.3, 
##     2) + poly(layer.4, 2)
## <environment: 0x7ff988e95038>
## 
## 
## Data table (top ten lines): 
## 
##       Longitude   Latitude   layer.1   layer.2   layer.3   layer.4   presence
## ---  ----------  ---------  --------  --------  --------  --------  ---------
## 1      -80.0106    21.8744      2765      1235      1174       252          1
## 2      -79.9086    21.8095      2289      1732       957       231          1
## 4      -79.8251    21.8095      2207      1877       967       259          1
## 5      -79.8807    21.8374      2244      1828       945       249          1
## 6      -79.9550    21.8374      2250      1766       919       235          1
## 7      -80.3446    22.0136      2201      1822       978       277          1
## 8      -80.2983    21.9951      2214      1786       986       284          1
## 9      -80.1776    21.9023      2287      1722       992       266          1
## 10     -80.1591    21.9673      2984       965      1311       237          1
## 12     -80.1220    21.9301      2898      1033      1231       242          1
## 
## 
## Model:  
## Call:
## glm(formula = f, family = "binomial", data = analysis.df[, -c(1, 
##     2)], weights = weights)
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -0.31097  -0.14531  -0.09519  -0.05068   1.29698  
## 
## Coefficients:
##                     Estimate Std. Error z value Pr(>|z|)
## (Intercept)         -0.89851    0.72085  -1.246    0.213
## poly(layer.1, 2)1 -207.98579  145.60845  -1.428    0.153
## poly(layer.1, 2)2  -39.06832   72.86199  -0.536    0.592
## poly(layer.2, 2)1 -223.49108  149.33881  -1.497    0.135
## poly(layer.2, 2)2   26.29494   62.26777   0.422    0.673
## poly(layer.3, 2)1  -10.75113   85.13846  -0.126    0.900
## poly(layer.3, 2)2   20.46979   35.21966   0.581    0.561
## poly(layer.4, 2)1   -0.07627   22.80262  -0.003    0.997
## poly(layer.4, 2)2  -16.95613   23.23552  -0.730    0.466
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 33.271  on 1011  degrees of freedom
## Residual deviance: 25.864  on 1003  degrees of freedom
## AIC: 29.522
## 
## Number of Fisher Scoring iterations: 5
## 
## 
## 
## Model fit (training data):  class          : ModelEvaluation 
## n presences    : 12 
## n absences     : 1000 
## AUC            : 0.7805 
## cor            : 0.09591092 
## max TPR+TNR at : -0.2768806 
## 
## 
## Environment space model fit (training data):  class          : ModelEvaluation 
## n presences    : 12 
## n absences     : 10000 
## AUC            : 0.4973167 
## cor            : 0.008345601 
## max TPR+TNR at : 0.4311432 
## 
## 
## Proportion of data wittheld for model fitting:  0.2
## 
## Model fit (test data):  class          : ModelEvaluation 
## n presences    : 4 
## n absences     : 1000 
## AUC            : 0.736 
## cor            : 0.06657909 
## max TPR+TNR at : -0.7947912 
## 
## 
## Environment space model fit (test data):  class          : ModelEvaluation 
## n presences    : 4 
## n absences     : 10000 
## AUC            : 0.506075 
## cor            : 0.003566256 
## max TPR+TNR at : 0.3110623 
## 
## 
## Suitability:  
## class       : RasterLayer 
## dimensions  : 418, 1535, 641630  (nrow, ncol, ncell)
## resolution  : 0.008333333, 0.008333333  (x, y)
## extent      : -86.90809, -74.11642, 19.80837, 23.2917  (xmin, xmax, ymin, ymax)
## coord. ref. : NA 
## data source : in memory
## names       : layer 
## values      : 2.220446e-16, 1  (min, max)
## 
## 
## 
## Notes:

To check out the marginal response functions, you only need to type

ahli.glm$response.plots
## $layer.1

## 
## $layer.2

## 
## $layer.3

## 
## $layer.4

You can also visualize your models and data in a 2D environment space using any pair of layers from your environment stack. These plots hold all non-plotted variables (layer.1 and layer.3 in this case) constant at their mean value across all presence points, then vary the plotted variables between the minimum and maximum values in env.

The suit.plot shows you suitability in environment space as a function of your two variables, with brighter colors representing variable combinations predicted to be more suitable. The points represent the occurrence points for your species in that environment space.

The colored raster of the background.plot shows you the density of background points in environment space, while the white points again represent your occurrence points in environment space.

visualize.enm(ahli.glm, env, layers = c("layer.2", "layer.4"))
## $background.plot

## 
## $suit.plot

GAM, Bioclim, Domain, and Maxent

The procedure for building Bioclim, Domain, and Maxent models is similar to the procedure for GLMs, with the exception that you do not need to pass a formula to the model function. Note that running Maxent models requires a bit of extra setup; see dismo documentation for details.

ahli.gam <- enmtools.gam(ahli, env, test.prop = 0.2)
ahli.dm <- enmtools.dm(ahli, env, test.prop = 0.2)
ahli.bc <- enmtools.bc(ahli, env, test.prop = 0.2)
ahli.mx <- enmtools.maxent(ahli, env, test.prop = 0.2)

Metrics: breadth, correlation, and overlap

ENMTools provides a number of metrics for ENMs and for similarities between ENMs. These include measures of niche breadth, based on Levins(1968). An important caveat when interpreting these metrics is that they are driven to some (variable) extent by the availability of different combinations of environmental variables. As such they are more accurately interpreted as a measurment of the smoothness of the geographic distribution of suitability scores than as an estimate of the breadth of the fundamental niche; an organism with narrow fundamental niche breadth that nonetheless encompasses a set of environmental conditions that is quite common will have a high breadth when measured using ENMs, while having a low breadth in environment space.

raster.breadth(ahli.glm)
## $B1
## [1] 0.9013185
## 
## $B2
## [1] 0.28784

ENMTools also provides metrics for measuring similarity between ENMs. These include Schoener’s D (Schoener 1968), I (Warren et al. 2008), and the Spearman rank correlation coefficient between two rasters. While D and I are commonly used in the ENM literature, they may tend to overestimate similarity between ENMs when many grid cells are of similar values (e.g., when two species prefer different habitat but the region contains a great deal of habitat that is unsuitable for both).

raster.overlap(ahli.glm, allogus.glm)
## $D
## [1] 0.3872956
## 
## $I
## [1] 0.6435114
## 
## $rank.cor
## [1] 0.2397531

A new feature of the R version of ENMTools is that you can now use these same metrics in the n-dimensional space of all combinations of environmental variables, instead of restricting your measures of model similarity to those sets of conditions that appear in the training region. This is done by repeatedly drawing Latin hypercube samples from the space of all possible combinations of environmental variables given the min and max of each variable within the training region. ENMTools continues to draw samples until subsequent iterations differ by less than a specified tolerance value. Lower tolerance values result in more precise estimates of overlap, but can take much longer to calculate.

env.overlap(ahli.glm, allogus.glm, env, tolerance = .001)
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## $env.D
## [1] 0.2684671
## 
## $env.I
## [1] 0.5081348
## 
## $env.cor
## [1] -0.1070376
## 
## $env.D.plot

## 
## $env.I.plot

## 
## $env.cor.plot

Hypothesis testing

Niche identity or equivalency test

In this example, we will run a niche identity (also called equivalency) test, as in Warren et al. 2008. This test takes the presence points for a pair of species and randomly reassigns them to each species, then builds ENMs for these randomized occurrences. By doing this many times, we can estimate the probability distribution for ENM overlap between species under the null hypothesis that the two species’ occurrences in the environment are effectively a random draw from the same underlying distribution. Note that niche evolution is only one of many reasons why two species’ realized environmental distributions might cause departures from this null hypothesis. See Warren et al. 2014 for details.

To run an identity test, we need to decide what type of models we will build, how many replicates we will run, and (in the case of GLM) a model formula to use for empirical models and the Monte Carlo replicates. The resulting object contains the replicate models, p values, and plots of the results. Typically idenity tests are run with at least 99 replicates, but we are using a smaller number here for the sake of execution time.

NOTE: In order for models to be comparable, both empirical and pseudoreplicate models for the identity test are conducted with pseudoabsence points pooled for the two species being compared.

id.glm <- identity.test(species.1 = ahli, species.2 = allogus, env = env, type = "glm", nreps = 4)
id.glm
## 
## 
##  
## 
## Identity test ahli vs. allogus
## 
## objectentity test p-values:
##        D        I rank.cor    env.D    env.I  env.cor 
##      0.2      0.2      0.2      0.2      0.2      0.2 
## 
## 
## Replicates:
## 
## 
##                      D           I     rank.cor       env.D       env.I      env.cor
## ----------  ----------  ----------  -----------  ----------  ----------  -----------
## empirical    0.4275813   0.7053769   -0.4786687   0.0529072   0.1867073   -0.6666710
## rep 1        0.8157180   0.9732624    0.6664528   0.6799815   0.8928813    0.3343525
## rep 2        0.8246860   0.9768936    0.1257411   0.7756219   0.9409170    0.1582786
## rep 3        0.8961059   0.9914456    0.7679079   0.8570219   0.9744676    0.6715607
## rep 4        0.7468801   0.9552453    0.7450214   0.8095511   0.9545202    0.7409231
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 1 rows containing missing values (geom_bar).
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 1 rows containing missing values (geom_bar).

Background or similarity test

The background or similarity test compares the overlap seen between two species’ ENMs to the overlap expected by chance if one or both species was effectively choosing habitat at random from within their broad geographic range. The purpose of this test is to correct for the availability of habitat and ask whether the observed similarity between species or populations is significantly more (or less) than expected given the available set of environments in the regions in which they occur.

NOTE: In order for models to be comparable, both empirical and pseudoreplicate models for the background test are conducted with pseudoabsence points pooled for the two species being compared.

In Warren et al. 2008, we developed this test in the context of comparing one species’ actual occurrence to the random background occurrences of the other species. This is what we call an “asymmetric” test, and in our case we did the test in both directions with the idea that we might compare the results of A vs. B background to the results of B vs. A background. This may be informative in some cases, but many people have also found this asymmetry confusing (and indeed it is often difficult to interpret). For that reason, the background test here can be conducted against a null hypothesis that is generated from “asymmetric” (species.1 vs species.2 background) or “symmetric” (species.1 background vs. species.2 background) comparisons.

Here, for instance, is a Bioclim background test using the classical asymmetric approach:

bg.bc.asym <- background.test(species.1 = ahli, species.2 = allogus, env = env, type = "bc", nreps = 4, test.type = "asymmetric" )
bg.bc.asym
## 
## 
##  
## 
## Asymmetric background test
##  ahli vs. allogus background
## 
## background test p-values:
##        D        I rank.cor    env.D    env.I  env.cor 
##      0.4      0.4      0.4      0.4      0.4      0.4 
## 
## 
## Replicates:
## 
## 
##                      D           I    rank.cor       env.D       env.I     env.cor
## ----------  ----------  ----------  ----------  ----------  ----------  ----------
## empirical    0.1328502   0.3177390   0.0706201   0.0222270   0.1172144   0.0917132
## rep 1        0.1877225   0.3808244   0.2152289   0.0896843   0.2595781   0.2054716
## rep 2        0.1765302   0.3801102   0.2494760   0.0925981   0.2836106   0.2221921
## rep 3        0.1739254   0.3667993   0.2195958   0.0350236   0.1544975   0.1102229
## rep 4        0.1707311   0.3676270   0.2262568   0.0766575   0.2559650   0.2075789
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 1 rows containing missing values (geom_bar).
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 1 rows containing missing values (geom_bar).

And here is a Domain background test using the symmetric approach:

bg.dm.sym <- background.test(species.1 = ahli, species.2 = allogus, env = env, type = "dm", nreps = 4, test.type = "symmetric" )
bg.dm.sym
## 
## 
##  
## 
## Symmetric background test
##  ahli background vs. allogus background
## 
## background test p-values:
##        D        I rank.cor    env.D    env.I  env.cor 
##      0.4      0.4      0.4      0.4      0.4      0.4 
## 
## 
## Replicates:
## 
## 
##                      D           I    rank.cor       env.D       env.I     env.cor
## ----------  ----------  ----------  ----------  ----------  ----------  ----------
## empirical    0.4929334   0.7052122   0.2916150   0.1072190   0.3112276   0.2267641
## rep 1        0.8742330   0.9688595   0.5652197   0.1835585   0.4127667   0.3920586
## rep 2        0.8156434   0.9402023   0.7692109   0.2113470   0.4465616   0.3694611
## rep 3        0.9510865   0.9960125   0.8591934   0.4667844   0.6902162   0.6417635
## rep 4        0.9503246   0.9969945   0.7609138   0.6867803   0.8606787   0.7954232
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 1 rows containing missing values (geom_bar).
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 1 rows containing missing values (geom_bar).

Ecospat tests

Using enmtools.species objects also provides a simplified interface to the niche equivalency and similarity tests (or identity and background tests, respectively) that were developed by Broennimann et al. (2012). These tests do not rely on ENMs, instead using kernel density smoothing to estimate density of the species in environment space. Ecospat also uses the density of the available environment to correct for availability when measuring overlaps, so that overlaps are not strictly driven by availability of combinations of environmental variables.

These tests only work with two environmental axes, so they are often done with the top two PC axes of a set of environments. In our case we’ll just pick a couple of environmental layers, though (layer.1 and layer.2). Here’s an equivalency/identity test:

esp.id <- enmtools.ecospat.id(ahli, allogus, env[[c("layer.1", "layer.2")]])
esp.id
## 
## 
##  
## 
## Ecospat identity test ahli vs. allogus
## 
## ecospat.id test empirical overlaps:
## $D
## [1] 0.1721245
## 
## $I
## [1] 0.2918588
## 
## 
## 
## ecospat.id test p-values:
##    D    I 
## 0.49 0.73
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

## NULL

And here’s a symmetric background test. The difference between symmetric and asymmetric for these tests is the same as for the background tests presented above.

esp.bg.sym <- enmtools.ecospat.bg(ahli, allogus, env[[c("layer.1", "layer.3")]], test.type = "symmetric")
esp.bg.sym
## 
## 
##  
## 
## Ecospat background test symmetric ahli vs. allogus
## 
## ecospat.bg test empirical overlaps:
## $D
## [1] 0.09587224
## 
## $I
## [1] 0.2013701
## 
## 
## 
## ecospat.bg test p-values:
##    D    I 
## 0.34 0.32
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 7 rows containing non-finite values (stat_bin).

## NULL

Rangebreak tests

ENMTools also allows you to perform linear, blob, and ribbon rangebreak tests as developed in Glor and Warren 2011. The linear and blob tests are two versions of a test that permit one to ask whether the geographic regions occupied by two species are more environmentally different than expected by chance. The ribbon test, meanwhile, is designed to test whether the ranges of two species are divided by a region that is relatively unsuitable to one or both forms.

For the linear and blob tests, you call them very much like you would the identity and background tests. Here’s a linear one using GLM models:

rbl.glm <- rangebreak.linear(ahli, allogus, env, type = "glm", nreps = 4)
## 
## Building empirical models...
## Adding environmental data to species ahli 
##  Processing presence points...
##  Processing background points...
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Adding environmental data to species allogus 
##  Processing presence points...
##  Processing background points...
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## 
## Building replicate models...
## 
## Replicate 1 ...
## Adding environmental data to species ahli 
##  Processing presence points...
##  Processing background points...
## Adding environmental data to species allogus 
##  Processing presence points...
##  Processing background points...
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## 
## Replicate 2 ...
## Adding environmental data to species ahli 
##  Processing presence points...
##  Processing background points...
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Adding environmental data to species allogus 
##  Processing presence points...
##  Processing background points...
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## 
## Replicate 3 ...
## Adding environmental data to species ahli 
##  Processing presence points...
##  Processing background points...
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Adding environmental data to species allogus 
##  Processing presence points...
##  Processing background points...
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## 
## Replicate 4 ...
## Adding environmental data to species ahli 
##  Processing presence points...
##  Processing background points...
## Adding environmental data to species allogus 
##  Processing presence points...
##  Processing background points...
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
rbl.glm
## 
## 
##  
## 
## Linear rangebreak test ahli vs. allogus
## 
## rangebreak test p-values:
##        D        I rank.cor    env.D    env.I  env.cor 
##      0.8      0.8      0.8      0.6      0.6      0.4 
## 
## 
## Replicates:
## 
## 
##                      D           I     rank.cor       env.D       env.I      env.cor
## ----------  ----------  ----------  -----------  ----------  ----------  -----------
## empirical    0.4275813   0.7053769   -0.4786687   0.0531431   0.1866532   -0.6658198
## rep 1        0.4547356   0.7556077   -0.7637410   0.0647992   0.2013856   -0.7802382
## rep 2        0.4275813   0.7053769   -0.4786687   0.0525812   0.1860312   -0.6652681
## rep 3        0.4275813   0.7053769   -0.4786687   0.0529029   0.1863258   -0.6632512
## rep 4        0.2410662   0.5069245    0.8417841   0.6852343   0.8530080    0.8654194

## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 1 rows containing missing values (geom_bar).
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 1 rows containing missing values (geom_bar).

And here’s a blob test using Bioclim:

rbb.bc <- rangebreak.blob(ahli, allogus, env, type = "bc", nreps = 4)
## 
## Building empirical models...
## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## 
## Building replicate models...
## 
## Replicate 1 ...
## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## 
## Replicate 2 ...
## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## 
## Replicate 3 ...
## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## 
## Replicate 4 ...
## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude

## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
rbb.bc
## 
## 
##  
## 
## blob rangebreak test ahli vs. allogus
## 
## rangebreak test p-values:
##        D        I rank.cor    env.D    env.I  env.cor 
##      1.0      1.0      1.0      0.6      0.4      0.4 
## 
## 
## Replicates:
## 
## 
##                      D          I    rank.cor       env.D       env.I     env.cor
## ----------  ----------  ---------  ----------  ----------  ----------  ----------
## empirical    0.1328502   0.317739   0.0706201   0.0220170   0.1148683   0.0895757
## rep 1        0.1328502   0.317739   0.0706201   0.0209793   0.1119951   0.0868374
## rep 2        0.1328502   0.317739   0.0706201   0.0232263   0.1174997   0.0928786
## rep 3        0.1328502   0.317739   0.0706201   0.0215375   0.1151728   0.0899525
## rep 4        0.1328502   0.317739   0.0706201   0.0228552   0.1170299   0.0917533
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 1 rows containing missing values (geom_bar).
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 1 rows containing missing values (geom_bar).

If you want to access the individual replicates (for instance to see how your ranges are being split up), you can find them in the list named “replicate.models” inside your rangebreak test object.

rbl.glm$replicate.models$ahli.rep.1
## 
## 
## Formula:  presence ~ layer.1 + layer.2 + layer.3 + layer.4
## <environment: 0x7ff9718ba708>
## 
## 
## Data table (top ten lines): 
## 
##  Longitude   Latitude   layer.1   layer.2   layer.3   layer.4   presence
## ----------  ---------  --------  --------  --------  --------  ---------
##   -80.3446    22.0136      2201      1822       978       277          1
##   -80.2983    21.9951      2214      1786       986       284          1
##   -80.1498    21.9858      3042       841      1371       221          1
##   -80.1776    21.9673      2914      1020      1256       237          1
##   -80.1591    21.9673      2984       965      1311       237          1
##   -80.2148    21.9394      2329      1692      1018       269          1
##   -80.0437    21.9720      2712      1285      1126       250          1
##   -79.9972    21.9792      2861      1150      1194       259          1
##   -80.1220    21.9301      2898      1033      1231       242          1
##   -80.1776    21.9023      2287      1722       992       266          1
## 
## 
## Model:  
## Call:
## glm(formula = f, family = "binomial", data = analysis.df[, -c(1, 
##     2)], weights = weights)
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -0.20795  -0.12100  -0.09506  -0.03857   1.37508  
## 
## Coefficients:
##               Estimate Std. Error z value Pr(>|z|)  
## (Intercept) -24.894954  14.681906  -1.696    0.090 .
## layer.1       0.002951   0.003328   0.887    0.375  
## layer.2       0.006214   0.004322   1.438    0.151  
## layer.3       0.013294   0.010372   1.282    0.200  
## layer.4      -0.022177   0.017825  -1.244    0.213  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 44.361  on 2015  degrees of freedom
## Residual deviance: 34.693  on 2011  degrees of freedom
## AIC: 26.141
## 
## Number of Fisher Scoring iterations: 7
## 
## 
## 
## Model fit (training data):  class          : ModelEvaluation 
## n presences    : 16 
## n absences     : 2000 
## AUC            : 0.74875 
## cor            : 0.05497979 
## max TPR+TNR at : -0.4536585 
## 
## 
## Environment space model fit (training data):  class          : ModelEvaluation 
## n presences    : 16 
## n absences     : 10000 
## AUC            : 0.9295313 
## cor            : 0.08862697 
## max TPR+TNR at : 0.388415 
## 
## 
## Proportion of data wittheld for model fitting:  0
## 
## Model fit (test data):  [1] NA
## 
## 
## Environment space model fit (test data):  [1] NA
## 
## 
## Suitability:  
## class       : RasterLayer 
## dimensions  : 418, 1535, 641630  (nrow, ncol, ncell)
## resolution  : 0.008333333, 0.008333333  (x, y)
## extent      : -86.90809, -74.11642, 19.80837, 23.2917  (xmin, xmax, ymin, ymax)
## coord. ref. : NA 
## data source : in memory
## names       : layer 
## values      : 2.220446e-16, 0.9459527  (min, max)
## 
## 
## 
## Notes:

rbl.glm$replicate.models$allogus.rep.1
## 
## 
## Formula:  presence ~ layer.1 + layer.2 + layer.3 + layer.4
## <environment: 0x7ff98a05b778>
## 
## 
## Data table (top ten lines): 
## 
##  Longitude   Latitude   layer.1   layer.2   layer.3   layer.4   presence
## ----------  ---------  --------  --------  --------  --------  ---------
##   -79.8251    21.8095      2207      1877       967       259          1
##   -78.6189    22.2373      2317      1980      1065       374          1
##   -79.8065    21.7631      2158      1870       983       253          1
##   -77.9719    21.7091      2394      1789       966       364          1
##   -77.9323    21.6167      2384      1666      1017       324          1
##   -77.9719    21.5507      2402      1708       992       325          1
##   -78.1039    21.1809      2461      1669       846       252          1
##   -78.0247    21.1809      2437      1684       877       264          1
##   -77.9983    20.9301      2315      1746       907       265          1
##   -77.4569    20.9829      2417      1646       845       279          1
## 
## 
## Model:  
## Call:
## glm(formula = f, family = "binomial", data = analysis.df[, -c(1, 
##     2)], weights = weights)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -0.3252  -0.1877  -0.1255  -0.1024   1.8579  
## 
## Coefficients:
##               Estimate Std. Error z value Pr(>|z|)    
## (Intercept) 11.9815810  4.0657421   2.947 0.003209 ** 
## layer.1     -0.0031705  0.0009458  -3.352 0.000802 ***
## layer.2     -0.0023768  0.0008908  -2.668 0.007626 ** 
## layer.3     -0.0024424  0.0018047  -1.353 0.175950    
## layer.4      0.0030528  0.0018908   1.615 0.106403    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 180.22  on 3064  degrees of freedom
## Residual deviance: 149.85  on 3060  degrees of freedom
## AIC: 83.969
## 
## Number of Fisher Scoring iterations: 4
## 
## 
## 
## Model fit (training data):  class          : ModelEvaluation 
## n presences    : 65 
## n absences     : 3000 
## AUC            : 0.7654359 
## cor            : 0.1435551 
## max TPR+TNR at : 0.06973534 
## 
## 
## Environment space model fit (training data):  class          : ModelEvaluation 
## n presences    : 65 
## n absences     : 10000 
## AUC            : 0.33488 
## cor            : -0.02718397 
## max TPR+TNR at : 0.2451338 
## 
## 
## Proportion of data wittheld for model fitting:  0
## 
## Model fit (test data):  [1] NA
## 
## 
## Environment space model fit (test data):  [1] NA
## 
## 
## Suitability:  
## class       : RasterLayer 
## dimensions  : 418, 1535, 641630  (nrow, ncol, ncell)
## resolution  : 0.008333333, 0.008333333  (x, y)
## extent      : -86.90809, -74.11642, 19.80837, 23.2917  (xmin, xmax, ymin, ymax)
## coord. ref. : NA 
## data source : in memory
## names       : layer 
## values      : 0.06323003, 0.9290769  (min, max)
## 
## 
## 
## Notes:

For the ribbon rangebreak test, you will need one extra thing; a third enmtools.species object representing the occurrence points (for one or both species) that fall within the ribbon of putatively unsuitable habitat. In the case of these two anoles we don’t have such a ribbon, so we’ll just simulate one based on some random points.

ribbon <- enmtools.species(species.name = "ribbon")
ribbon$presence.points <- data.frame(Longitude = runif(n = 10, min = -79, max = -78.5),
                                      Latitude = runif(n = 10, min = 21.7, max = 22.1))
plot(env[[1]])
points(ribbon$presence.points, pch = 16)

ribbon$range <- background.raster.buffer(ribbon$presence.points, 20000, mask = env)
ribbon
## 
## 
## Range raster: 
## class       : RasterLayer 
## dimensions  : 418, 1535, 641630  (nrow, ncol, ncell)
## resolution  : 0.008333333, 0.008333333  (x, y)
## extent      : -86.90809, -74.11642, 19.80837, 23.2917  (xmin, xmax, ymin, ymax)
## coord. ref. : NA 
## data source : in memory
## names       : layer.1 
## values      : 1, 1  (min, max)
## 
## 
## 
## Presence points (first ten only): 
## 
##  Longitude   Latitude
## ----------  ---------
##  -78.92446   22.03760
##  -78.80965   21.81070
##  -78.93151   21.73836
##  -78.81453   22.06297
##  -78.90457   21.81125
##  -78.63598   22.07720
##  -78.82587   21.76030
##  -78.83197   21.79681
##  -78.84890   21.87492
##  -78.84955   22.08452
## 
## 
## Background points not defined.
## 
## Species name:  ribbon

Now we’ll run a ribbon rangebreak test using GLM models with quadratic effects. We also need to tell it the width of the ribbons to generate for the replicates. The units for the width argument are the same units that the presence points are in; e.g., if the points are in decimal degrees you should supply the width of the barrier in decimal degrees.

rbr.glm <- rangebreak.ribbon(ahli, allogus, ribbon, env, type = "glm", f = pres ~ poly(layer.1, 2) + poly(layer.2, 2) + poly(layer.3, 2) + poly(layer.4, 2), width = 0.5, nreps = 4)
## 
## 
## No background points provided, drawing background from range raster.
## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude
## 
## Building empirical models...
## Adding environmental data to species allogus 
##  Processing presence points...
##  Processing background points...
## Adding environmental data to species ahli 
##  Processing presence points...
##  Processing background points...
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Adding environmental data to species ribbon 
##  Processing presence points...
##  Processing background points...
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Adding environmental data to species outside 
##  Processing presence points...
##  Processing background points...
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## 
## Building replicate models...
## 
## Replicate 1 ...
## Adding environmental data to species allogus 
##  Processing presence points...
##  Processing background points...
## Adding environmental data to species ahli 
##  Processing presence points...
##  Processing background points...
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Adding environmental data to species ribbon 
##  Processing presence points...
##  Processing background points...
## Adding environmental data to species outside 
##  Processing presence points...
##  Processing background points...
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## 
## Replicate 2 ...
## Adding environmental data to species allogus 
##  Processing presence points...
##  Processing background points...
## Adding environmental data to species ahli 
##  Processing presence points...
##  Processing background points...
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Adding environmental data to species ribbon 
##  Processing presence points...
##  Processing background points...
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Adding environmental data to species outside 
##  Processing presence points...
##  Processing background points...
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## 
## Replicate 3 ...
## Adding environmental data to species allogus 
##  Processing presence points...
##  Processing background points...
## Adding environmental data to species ahli 
##  Processing presence points...
##  Processing background points...
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Adding environmental data to species ribbon 
##  Processing presence points...
##  Processing background points...
## Adding environmental data to species outside 
##  Processing presence points...
##  Processing background points...
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## 
## Replicate 4 ...
## Adding environmental data to species allogus 
##  Processing presence points...
##  Processing background points...
## Adding environmental data to species ahli 
##  Processing presence points...
##  Processing background points...
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Adding environmental data to species ribbon 
##  Processing presence points...
##  Processing background points...
## Adding environmental data to species outside 
##  Processing presence points...
##  Processing background points...
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
## [1] "Trying to find starting conditions, attempt 1"
## [1] "Building replicates..."
rbr.glm
## 
## 
##  
## 
## ribbon rangebreak test allogus vs. ahli
## 
## rangebreak test p-values...
## 
## Species 1 vs. Species 2:
##        D        I rank.cor    env.D    env.I  env.cor 
##      0.4      0.4      0.8      1.2      1.2      0.4 
## 
## Species 1 vs. Ribbon:
##        D        I rank.cor    env.D    env.I  env.cor 
##      1.2      1.2      0.4      0.8      1.2      1.2 
## 
## Species 2 vs. Ribbon:
##        D        I rank.cor    env.D    env.I  env.cor 
##      0.4      0.4      0.4      0.8      0.4      0.4 
## 
## Outside vs. Ribbon:
##        D        I rank.cor    env.D    env.I  env.cor 
##      1.2      1.2      0.4      0.4      0.4      0.8 
## 
## 
## Replicates:
## 
## Species 1 vs. Species 2:
##                    D          I   rank.cor      env.D     env.I
## empirical 0.37788432 0.60834910 -0.0804527 0.10081287 0.2520540
## rep 1     0.13030863 0.34957341  0.2687061 0.08171997 0.2813365
## rep 2     0.35180884 0.59227941 -0.2502273 0.09134668 0.2457501
## rep 3     0.01312460 0.08707379  0.1259064 0.15923975 0.2200309
## rep 4     0.01592278 0.10205162  0.1876094 0.18016032 0.2603828
##               env.cor
## empirical  0.05022599
## rep 1     -0.01320386
## rep 2      0.03327966
## rep 3     -0.58871209
## rep 4     -0.53924456
## 
## Species 1 vs. Ribbon:
##                    D         I    rank.cor     env.D     env.I     env.cor
## empirical 0.13373066 0.3374857 -0.62129216 0.2251050 0.4052149 -0.02864767
## rep 1     0.27428327 0.5571513  0.82036027 0.6083392 0.8126020  0.39202612
## rep 2     0.63135659 0.8368148 -0.05754995 0.3545114 0.5889051  0.36758623
## rep 3     0.08447882 0.2581315  0.37447076 0.2208341 0.3169973 -0.70021560
## rep 4     0.08125962 0.2599869  0.52757707 0.2521230 0.3693201 -0.64259530
## 
## Species 2 vs. Ribbon:
##                   D         I  rank.cor     env.D     env.I   env.cor
## empirical 0.2633291 0.4639691 0.1209519 0.2295097 0.3508640 0.1504447
## rep 1     0.3424765 0.5441930 0.6329861 0.1481116 0.3769520 0.3242770
## rep 2     0.5587035 0.7958683 0.7712128 0.2918676 0.5221257 0.7676180
## rep 3     0.4077782 0.6589963 0.5145023 0.7584858 0.8782296 0.8903068
## rep 4     0.4646606 0.7076767 0.5128828 0.7529127 0.8598187 0.8595882
## 
## Outside vs. Ribbon:
##                   D         I    rank.cor     env.D     env.I     env.cor
## empirical 0.1486587 0.3690104 -0.75110361 0.1291248 0.3183805 -0.49360830
## rep 1     0.2385524 0.5189814  0.44432580 0.5139175 0.7237600 -0.05937359
## rep 2     0.6622354 0.8571910  0.06805449 0.2579555 0.4905987 -0.21605855
## rep 3     0.1042397 0.3053085  0.60606136 0.2958021 0.4186270 -0.47472909
## rep 4     0.1080649 0.3260272  0.71455369 0.3243423 0.5013225 -0.67820567
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 1 rows containing missing values (geom_bar).
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 1 rows containing missing values (geom_bar).

## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 1 rows containing missing values (geom_bar).
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 1 rows containing missing values (geom_bar).

## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 1 rows containing missing values (geom_bar).
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 1 rows containing missing values (geom_bar).

## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 1 rows containing missing values (geom_bar).
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 1 rows containing missing values (geom_bar).

Note that the output table here has slope, intercept, and intercept offset.

rbr.glm$lines.df
##        slope intercept    offset
## 1  0.1217436  29.32230 0.2518459
## 2 -1.0093269 -57.79823 0.3552060
## 3 -0.8986740 -46.90349 0.3361189
## 4 -1.3430022 -80.12773 0.4186029

The intercept denotes the intercept corresponding to the CENTER of each ribbon. To get the lines denoting the edges of the ribbons (for example if you want to plot the ribbons on a map), you add and substract the offset. In other words, the top edge of the ribbon is given by y = (slope * x) + intercept + offset, while the bottom edge is given by y = (slope * x) + intercept - offset.

Building an enmtools.clade object

Some of the tests in ENMTools, including some really neat ones that are still in development, require you to build an enmtools.clade object. These objects are simply lists that contain a phylogeny and a set of enmtools.species objects. It’s important that the names of the species objects and their species.name attributes match the names in the phylogeny’s tip.labels. For demonstration, we’re going to build an object for a clade of five anoles from Hispaniola. We have the tree, so we’re just going to grab occurrence data from GBIF using the rgbif package.

library(rgbif)
library(ape)
## 
## Attaching package: 'ape'
## The following objects are masked from 'package:raster':
## 
##     rotate, zoom
hisp.anoles <- read.nexus(file = "test/testdata/StarBEAST_MCC.species.txt")

keepers <- c("brevirostris", "marron", "caudalis", "websteri", "distichus")

hisp.anoles <- drop.tip(phy = hisp.anoles, tip = hisp.anoles$tip.label[!hisp.anoles$tip.label %in% keepers])
plot(hisp.anoles)

So there’s our tree. Now we’re going to grab some environmental data.

hisp.env <- stack(list.files("test/testdata/Hispaniola_Worldclim", full.names = TRUE))
hisp.env <- setMinMax(hisp.env)

And then we’ll create a function to build species from GBIF.

# Automate the process of downloading data and removing duds and dupes
species.from.gbif <- function(genus, species, name = NA, env){

  # Name it after the species epithet unless told otherwise
  if(is.na(name)){
    name <- species
  }

  # Get GBIF data
  this.sp <- enmtools.species(presence.points = gbif(genus = genus, species = species)[,c("lon", "lat")],
                              species.name = name)

  # Rename columns, get rid of duds
  colnames(this.sp$presence.points) <- c("Longitude", "Latitude")
  this.sp$presence.points <- this.sp$presence.points[complete.cases(extract(env, this.sp$presence.points)),]
  this.sp$presence.points <- this.sp$presence.points[!duplicated(this.sp$presence.points),]

  this.sp$range <- background.raster.buffer(this.sp$presence.points, 50000, mask = hisp.env)

  return(this.sp)
}

Now we’ll create five species and add them to a species.clade object that is called brev.clade.

brevirostris <- species.from.gbif(genus = "Anolis", species = "brevirostris", env = hisp.env)
marron <- species.from.gbif(genus = "Anolis", species = "marron", env = hisp.env)
caudalis <- species.from.gbif(genus = "Anolis", species = "caudalis", env = hisp.env)
websteri <- species.from.gbif(genus = "Anolis", species = "websteri", env = hisp.env)
distichus <- species.from.gbif(genus = "Anolis", species = "distichus", env = hisp.env)


brev.clade <- enmtools.clade(species = list(brevirostris, marron, caudalis, websteri, distichus), tree = hisp.anoles)
check.clade(brev.clade)
## 
## 
## An enmtools.clade object with 5 species
## 
## Species names: 
##   brevirostris    caudalis    distichus   marron      websteri
## 
## Tree: 
## 
## Phylogenetic tree with 5 tips and 4 internal nodes.
## 
## Tip labels:
## [1] "brevirostris" "caudalis"     "distichus"    "marron"      
## [5] "websteri"    
## 
## Rooted; includes branch lengths.
## 
## 
## Data Summary: 
## 
## 
##                species.names   in.tree   presence   background   range   
## -------------  --------------  --------  ---------  -----------  --------
## brevirostris   brevirostris    TRUE      173        0            present 
## caudalis       caudalis        TRUE      21         0            present 
## distichus      distichus       TRUE      668        0            present 
## marron         marron          TRUE      12         0            present 
## websteri       websteri        TRUE      17         0            present

Age-overlap correlation tests (AOC)

The AOC tests allow you to examine patterns of range, point, and ENM overlap in the context of a phylogeny. This is effectively a generalized version of several analyses: age-range correlation (e.g., Fitzpatrick and Turelli 2006), ENM overlap in the context of a phylogeny (e.g., Knouft et al. 2006, Warren et al. 2008), and point overlaps (e.g., Cardillo and Warren 2016).

These tests require the creation of an enmtools.clade object, as above. AOC tests consist of two steps: first, the average overlap at each node in the phylogeny is calcualted using a method that takes tree topology into account (see Fitzpatrick and Turelli 2006), then we perform a linear regression to measure the relationship between node age and average overlap. Due to the fact that these overlaps violate many of the assumptions of a regular linear regression, however (e.g., errors are not iid), we can’t calculate significance in the typical way. Instead we performa Monte Carlo test, permuting the identity of the tips of the tree and repeating the node averaging and modeling steps. Finally we measure statistical significance by comparing the empirical slope and intercept to the distribution of slopes and intercepts from the Monte Carlo replicates.

First, let’s do one using geog.range.overlaps, as in Fitzpatrick and Turelli 2006. Note that this analysis requires that each of your species have a range raster stored in their species object (we did that as part of the function used above).

range.aoc <- enmtools.aoc(clade = brev.clade,  nreps = 50, overlap.source = "range")
summary(range.aoc)
## 
## 
## Age-Overlap Correlation test
## 
## 50 replicates 
## 
## p values:
##      (Intercept) empirical.df$age 
##        0.1960784        0.1960784
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

## NULL

Now we can do one using point overlaps just by changing the overlap.source argument:

point.aoc <- enmtools.aoc(clade = brev.clade,  nreps = 50, overlap.source = "points")
summary(point.aoc)
## 
## 
## Age-Overlap Correlation test
## 
## 50 replicates 
## 
## p values:
##      (Intercept) empirical.df$age 
##       0.03921569       0.03921569
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

## NULL

Or we can use similarity between ENMs built for each species. Here we’ll use GLM models:

glm.aoc <- enmtools.aoc(clade = brev.clade,  env = hisp.env, nreps = 50, overlap.source = "glm", f = pres ~ snv_1 + snv_12)
## 
## 
## No background points provided, drawing background from range raster.
## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude
## Adding environmental data to species brevirostris 
##  Processing presence points...
##  Processing background points...
## 
## 
## No background points provided, drawing background from range raster.
## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude
## Adding environmental data to species caudalis 
##  Processing presence points...
##  Processing background points...
## 
## 
## No background points provided, drawing background from range raster.
## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude
## Adding environmental data to species distichus 
##  Processing presence points...
##  Processing background points...
## 
## 
## No background points provided, drawing background from range raster.
## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude
## Adding environmental data to species marron 
##  Processing presence points...
##  Processing background points...
## 
## 
## No background points provided, drawing background from range raster.
## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude
## Adding environmental data to species websteri 
##  Processing presence points...
##  Processing background points...
summary(glm.aoc)
## 
## 
## Age-Overlap Correlation test
## 
## 50 replicates 
## 
## p values:
##      (Intercept) empirical.df$age 
##        0.2352941        0.2352941
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

## NULL

Literature cited

*Broennimann, O., Fitzpatrick, M. C., Pearman, P. B., Petitpierre, B., Pellissier, L., Yoccoz, N. G., Thuiller, W., Fortin, M.-J., Randin, C., Zimmermann, N. E., Graham, C. H. and Guisan, A. (2012), Measuring ecological niche overlap from occurrence and spatial environmental data. Global Ecology and Biogeography, 21: 481–497. doi:10.1111/j.1466-8238.2011.00698.x*

Fitzpatrick, B. M., & Turelli, M. (2006). The geography of mammalian speciation: mixed signals from phylogenies and range maps. Evolution, 60(3), 601-615.

Knouft, J. H., Losos, J. B., Glor, R. E., & Kolbe, J. J. (2006). Phylogenetic analysis of the evolution of the niche in lizards of the Anolis sagrei group. Ecology, 87(sp7).

Levins, R. 1968. Evolution In Changing Environments. Monographs in Population Biology, volume 2. Princeton University Press, Princeton, New Jersey, USA.

Schoener, T. W. 1968. Anolis lizards of Bimini: resource partitioning in a complex fauna. Ecology 49:704- 726.

Warren, D.L., R.E. Glor, and M. Turelli. 2008. Environmental niche identity versus conservatism: quantitative approaches to niche evolution. Evolution 62:2868-2883. doi: 10.1111/j.1558-5646.2008.00482.x

Warren, D.L., M. Cardillo, D.F. Rosauer, and D.I. Bolnick. 2014. Mistaking geography for biology: inferring processes from species distributions. Trends in Ecology and Evolution 29 (10), 572-580. doi: 10.1016/j.tree.2014.08.003